/*
 * Copyright (c) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <cassert>
#include <fcntl.h>
#include <gtest/gtest.h>
#include <singleton.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "file_uri.h"

namespace {
    using namespace std;
    using namespace OHOS::AppFileService::ModuleFileUri;

    const int32_t E_OK = 0;

    class FileUriTest : public testing::Test {
    public:
        static void SetUpTestCase(void) {};
        static void TearDownTestCase() {};
        void SetUp() {};
        void TearDown() {};
    };

    /**
     * @tc.name: File_Uri_TransToFileUri_0000
     * @tc.desc: Test function of TransToFileUri() interface for SUCCESS.
     * @tc.size: MEDIUM
     * @tc.type: FUNC
     * @tc.level Level 1
     * @tc.require: SR000H63TL
     */
    HWTEST_F(FileUriTest, File_Uri_TransToFileUri_0000, testing::ext::TestSize.Level1)
    {
        GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_TransToFileUri_0000";
        string dataShareUri = "datashare://1002/media/image/12";
        string resultUri = "file://media/image/12#networkid=1002";
        string fileUri;
        int32_t ret = FileUri::TransToFileUri(dataShareUri, fileUri);
        ASSERT_TRUE(ret == E_OK) << "fileUri is " << fileUri;
        EXPECT_EQ(fileUri, resultUri);
        GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToFileUri_0000";
    }

    /**
     * @tc.name: File_Uri_TransToFileUri_0001
     * @tc.desc: Test function of TransToFileUri() interface for SUCCESS.
     * @tc.size: MEDIUM
     * @tc.type: FUNC
     * @tc.level Level 1
     * @tc.require: SR000H63TL
     */
    HWTEST_F(FileUriTest, File_Uri_TransToFileUri_0001, testing::ext::TestSize.Level1)
    {
        GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_TransToFileUri_0001";
        string dataShareUri = "datashare:///media/image/12";
        string resultUri = "file://media/image/12";
        string fileUri;
        int32_t ret = FileUri::TransToFileUri(dataShareUri, fileUri);
        ASSERT_TRUE(ret == E_OK) << "fileUri is " << fileUri;
        EXPECT_EQ(fileUri, resultUri);
        GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToFileUri_0001";
    }

    /**
     * @tc.name: File_Uri_TransToFileUri_0002
     * @tc.desc: Test function of TransToFileUri() interface for fail.
     * @tc.size: MEDIUM
     * @tc.type: FUNC
     * @tc.level Level 1
     * @tc.require: SR000H63TL
     */
    HWTEST_F(FileUriTest, File_Uri_TransToFileUri_0002, testing::ext::TestSize.Level1)
    {
        GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_TransToFileUri_0002";
        string dataShareUri = "datashare:////#fdFromBinder=12";
        string resultUri = "";
        string fileUri;
        int32_t ret = FileUri::TransToFileUri(dataShareUri, fileUri);
        ASSERT_TRUE(ret < E_OK) << "fileUri is empty! error: " << to_string(ret);
        EXPECT_EQ(fileUri, resultUri);
        GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToFileUri_0002";
    }

    /**
     * @tc.name: File_Uri_TransToFileUri_0003
     * @tc.desc: Test function of TransToFileUri() interface for fail.
     * @tc.size: MEDIUM
     * @tc.type: FUNC
     * @tc.level Level 1
     * @tc.require: SR000H63TL
     */
    HWTEST_F(FileUriTest, File_Uri_TransToFileUri_0003, testing::ext::TestSize.Level1)
    {
        GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_TransToFileUri_0003";
        string dataShareUri = "datashare:////";
        string resultUri = "";
        string fileUri;
        int32_t ret = FileUri::TransToFileUri(dataShareUri, fileUri);
        ASSERT_TRUE(ret < E_OK) << "fileUri is empty! error: " << to_string(ret);
        EXPECT_EQ(fileUri, resultUri);
        GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToFileUri_0003";
    }

    /**
     * @tc.name: File_Uri_TransToFileUri_0004
     * @tc.desc: Test function of TransToFileUri() interface for fail.
     * @tc.size: MEDIUM
     * @tc.type: FUNC
     * @tc.level Level 1
     * @tc.require: SR000H63TL
     */
    HWTEST_F(FileUriTest, File_Uri_TransToFileUri_0004, testing::ext::TestSize.Level1)
    {
        GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_TransToFileUri_0004";
        string dataShareUri = "datashare:/media/image/12";
        string resultUri = "";
        string fileUri;
        int32_t ret = FileUri::TransToFileUri(dataShareUri, fileUri);
        ASSERT_TRUE(ret < E_OK) << "fileUri is empty! error: " << to_string(ret);
        EXPECT_EQ(fileUri, resultUri);
        GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToFileUri_0004";
    }

    /**
     * @tc.name: File_Uri_TransToFileUri_0005
     * @tc.desc: Test function of TransToFileUri() interface for fail.
     * @tc.size: MEDIUM
     * @tc.type: FUNC
     * @tc.level Level 1
     * @tc.require: SR000H63TL
     */
    HWTEST_F(FileUriTest, File_Uri_TransToFileUri_0005, testing::ext::TestSize.Level1)
    {
        GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_TransToFileUri_0005";
        string dataShareUri = "datashar:///media/image/12";
        string resultUri = "";
        string fileUri;
        int32_t ret = FileUri::TransToFileUri(dataShareUri, fileUri);
        ASSERT_TRUE(ret < E_OK) << "fileUri is empty! error: " << to_string(ret);
        EXPECT_EQ(fileUri, resultUri);
        GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToFileUri_0005";
    }

    /**
     * @tc.name: File_Uri_TransToFileUri_0006
     * @tc.desc: Test function of TransToFileUri() interface for success.
     * @tc.size: MEDIUM
     * @tc.type: FUNC
     * @tc.level Level 1
     * @tc.require: SR000H63TL
     */
    HWTEST_F(FileUriTest, File_Uri_TransToFileUri_0006, testing::ext::TestSize.Level1)
    {
        GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_TransToFileUri_0006";
        string dataShareUri = "datashare://1/media/image/12?operation=thumbnail&width=256&height=256";
        string fileUri = "file://media/image/12?operation=thumbnail&width=256&height=256#networkid=1";
        string resultUri;
        int32_t ret = FileUri::TransToFileUri(dataShareUri, resultUri);
        ASSERT_TRUE(ret == E_OK) << "resultUri is " << resultUri;
        EXPECT_EQ(fileUri, resultUri);
        GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToFileUri_0006";
    }

    /**
     * @tc.name: File_Uri_TransToFileUri_0007
     * @tc.desc: Test function of TransToFileUri() interface for success.
     * @tc.size: MEDIUM
     * @tc.type: FUNC
     * @tc.level Level 1
     * @tc.require: SR000H63TL
     */
    HWTEST_F(FileUriTest, File_Uri_TransToFileUri_0007, testing::ext::TestSize.Level1)
    {
        GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_TransToFileUri_0007";
        string dataShareUri = "datashare:///media/image/12?operation=thumbnail&width=256&height=256";
        string fileUri = "file://media/image/12?operation=thumbnail&width=256&height=256";
        string resultUri;
        int32_t ret = FileUri::TransToFileUri(dataShareUri, resultUri);
        ASSERT_TRUE(ret == E_OK) << "resultUri is " << resultUri;
        EXPECT_EQ(fileUri, resultUri);
        GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToFileUri_0007";
    }

    /**
     * @tc.name: File_Uri_TransToDataShareUri_0008
     * @tc.desc: Test function of TransToDataShareUri() interface for SUCCESS.
     * @tc.size: MEDIUM
     * @tc.type: FUNC
     * @tc.level Level 1
     * @tc.require: SR000H63TL
     */
    HWTEST_F(FileUriTest, File_Uri_TransToDataShareUri_0008, testing::ext::TestSize.Level1)
    {
        GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_TransToDataShareUri_0008";
        string fileUri = "file://media/image/12#networkid=1002";
        string resultUri = "datashare://1002/media/image/12";
        string dataShareUri;
        int32_t ret = FileUri::TransToDataShareUri(fileUri, dataShareUri);
        ASSERT_TRUE(ret == E_OK) << "dataShareUri is " << dataShareUri;
        EXPECT_EQ(dataShareUri, resultUri);
        GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToDataShareUri_0008";
    }

    /**
     * @tc.name: File_Uri_TransToDataShareUri_0009
     * @tc.desc: Test function of TransToDataShareUri() interface for SUCCESS.
     * @tc.size: MEDIUM
     * @tc.type: FUNC
     * @tc.level Level 1
     * @tc.require: SR000H63TL
     */
    HWTEST_F(FileUriTest, File_Uri_TransToDataShareUri_0009, testing::ext::TestSize.Level1)
    {
        GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_TransToDataShareUri_0009";
        string fileUri = "file://media/image/12";
        string resultUri = "datashare:///media/image/12";
        string dataShareUri;
        int32_t ret = FileUri::TransToDataShareUri(fileUri, dataShareUri);
        ASSERT_TRUE(ret == E_OK) << "dataShareUri is " << dataShareUri;
        EXPECT_EQ(dataShareUri, resultUri);
        GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToDataShareUri_0009";
    }

    /**
     * @tc.name: File_Uri_TransToDataShareUri_0010
     * @tc.desc: Test function of TransToDataShareUri() interface for fail.
     * @tc.size: MEDIUM
     * @tc.type: FUNC
     * @tc.level Level 1
     * @tc.require: SR000H63TL
     */
    HWTEST_F(FileUriTest, File_Uri_TransToDataShareUri_0010, testing::ext::TestSize.Level1)
    {
        GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_TransToDataShareUri_0010";
        string fileUri = "file:////#fdFromBinder=12";
        string resultUri = "";
        string dataShareUri;
        int32_t ret = FileUri::TransToDataShareUri(fileUri, dataShareUri);
        ASSERT_TRUE(ret < E_OK) << "dataShareUri is empty! error: " << to_string(ret);
        EXPECT_EQ(dataShareUri, resultUri);
        GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToDataShareUri_0010";
    }

    /**
     * @tc.name: File_Uri_TransToDataShareUri_0011
     * @tc.desc: Test function of TransToDataShareUri() interface for fail.
     * @tc.size: MEDIUM
     * @tc.type: FUNC
     * @tc.level Level 1
     * @tc.require: SR000H63TL
     */
    HWTEST_F(FileUriTest, File_Uri_TransToDataShareUri_0011, testing::ext::TestSize.Level1)
    {
        GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_TransToDataShareUri_0011";
        string fileUri = "file://";
        string resultUri = "";
        string dataShareUri;
        int32_t ret = FileUri::TransToDataShareUri(fileUri, dataShareUri);
        ASSERT_TRUE(ret < E_OK) << "dataShareUri is empty! error: " << to_string(ret);
        EXPECT_EQ(dataShareUri, resultUri);
        GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToDataShareUri_0011";
    }

    /**
     * @tc.name: File_Uri_TransToDataShareUri_0012
     * @tc.desc: Test function of TransToDataShareUri() interface for fail.
     * @tc.size: MEDIUM
     * @tc.type: FUNC
     * @tc.level Level 1
     * @tc.require: SR000H63TL
     */
    HWTEST_F(FileUriTest, File_Uri_TransToDataShareUri_0012, testing::ext::TestSize.Level1)
    {
        GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_TransToDataShareUri_0012";
        string fileUri = "file:/media/image/12";
        string resultUri = "";
        string dataShareUri;
        int32_t ret = FileUri::TransToDataShareUri(fileUri, dataShareUri);
        ASSERT_TRUE(ret < E_OK) << "dataShareUri is empty! error: " << to_string(ret);
        EXPECT_EQ(dataShareUri, resultUri);
        GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToDataShareUri_0012";
    }

    /**
     * @tc.name: File_Uri_TransToDataShareUri_0013
     * @tc.desc: Test function of TransToDataShareUri() interface for fail.
     * @tc.size: MEDIUM
     * @tc.type: FUNC
     * @tc.level Level 1
     * @tc.require: SR000H63TL
     */
    HWTEST_F(FileUriTest, File_Uri_TransToDataShareUri_0013, testing::ext::TestSize.Level1)
    {
        GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_TransToDataShareUri_0013";
        string fileUri = "fil://media/image/12";
        string resultUri = "";
        string dataShareUri;
        int32_t ret = FileUri::TransToDataShareUri(fileUri, dataShareUri);
        ASSERT_TRUE(ret < E_OK) << "dataShareUri is empty! error: " << to_string(ret);
        EXPECT_EQ(dataShareUri, resultUri);
        GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToDataShareUri_0013";
    }

    /**
     * @tc.name: File_Uri_TransToDataShareUri_0014
     * @tc.desc: Test function of TransToDataShareUri() interface for success.
     * @tc.size: MEDIUM
     * @tc.type: FUNC
     * @tc.level Level 1
     * @tc.require: SR000H63TL
     */
    HWTEST_F(FileUriTest, File_Uri_TransToDataShareUri_0014, testing::ext::TestSize.Level1)
    {
        GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_TransToDataShareUri_0014";
        string fileUri = "file://media/image/12?operation=thumbnail&width=256&height=256#networkId=1";
        string dataShareUri = "datashare://1/media/image/12?operation=thumbnail&width=256&height=256";
        string resultUri;
        int32_t ret = FileUri::TransToDataShareUri(fileUri, resultUri);
        ASSERT_TRUE(ret == E_OK) << "resultUri is " << resultUri;
        EXPECT_EQ(dataShareUri, resultUri);
        GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToDataShareUri_0014";
    }

    /**
     * @tc.name: File_Uri_TransToDataShareUri_0015
     * @tc.desc: Test function of TransToDataShareUri() interface for success.
     * @tc.size: MEDIUM
     * @tc.type: FUNC
     * @tc.level Level 1
     * @tc.require: SR000H63TL
     */
    HWTEST_F(FileUriTest, File_Uri_TransToDataShareUri_0015, testing::ext::TestSize.Level1)
    {
        GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_TransToDataShareUri_0015";
        string fileUri = "file://media/image/12?operation=thumbnail&width=256&height=256";
        string dataShareUri = "datashare:///media/image/12?operation=thumbnail&width=256&height=256";
        string resultUri;
        int32_t ret = FileUri::TransToDataShareUri(fileUri, resultUri);
        ASSERT_TRUE(ret == E_OK) << "resultUri is " << resultUri;
        EXPECT_EQ(dataShareUri, resultUri);
        GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_TransToDataShareUri_0015";
    }

    /**
     * @tc.name: File_Uri_GetIdFromUri_0016
     * @tc.desc: Test function of GetIdFromUri() interface for success.
     * @tc.size: MEDIUM
     * @tc.type: FUNC
     * @tc.level Level 1
     * @tc.require: SR000H63TL
     */
    HWTEST_F(FileUriTest, File_Uri_GetIdFromUri_0016, testing::ext::TestSize.Level1)
    {
        GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_GetIdFromUri_0016";
        string dataShareUri = "datashare:///media/image/12";
        string dataShareId = "12";
        string dataShareResId;
        int32_t ret = FileUri::GetIdFromUri(dataShareUri, dataShareResId);
        ASSERT_TRUE(ret == E_OK) << "dataShareResId is " << dataShareResId;
        EXPECT_EQ(dataShareResId, dataShareId);

        string fileUri = "file://media/image/7";
        string fileId = "7";
        string fileResId;
        ret = FileUri::GetIdFromUri(fileUri, fileResId);
        ASSERT_TRUE(ret == E_OK) << "fileResId is " << fileResId;
        EXPECT_EQ(fileResId, fileId);
        GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_GetIdFromUri_0016";
    }

    /**
     * @tc.name: File_Uri_GetIdFromUri_0017
     * @tc.desc: Test function of GetIdFromUri() interface for fail.
     * @tc.size: MEDIUM
     * @tc.type: FUNC
     * @tc.level Level 1
     * @tc.require: SR000H63TL
     */
    HWTEST_F(FileUriTest, File_Uri_GetIdFromUri_0017, testing::ext::TestSize.Level1)
    {
        GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_GetIdFromUri_0017";
        string dataShareUri = "datashare:///media/image";
        string dataShareId = "";
        string dataShareResId;
        int32_t ret = FileUri::GetIdFromUri(dataShareUri, dataShareResId);
        ASSERT_TRUE(ret < E_OK) << "dataShareResId is empty! error:" << to_string(ret);
        EXPECT_EQ(dataShareResId, dataShareId);

        string fileUri = "file://media/image";
        string fileId = "";
        string fileResId;
        ret = FileUri::GetIdFromUri(fileUri, fileResId);
        ASSERT_TRUE(ret < E_OK) << "fileResId is empty! error:" << to_string(ret);
        EXPECT_EQ(fileResId, fileId);
        GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_GetIdFromUri_0017";
    }

    /**
     * @tc.name: File_Uri_GetNetWorkIdFromUri_0018
     * @tc.desc: Test function of GetNetWorkIdFromUri() interface for success.
     * @tc.size: MEDIUM
     * @tc.type: FUNC
     * @tc.level Level 1
     * @tc.require: SR000H63TL
     */
    HWTEST_F(FileUriTest, File_Uri_GetNetWorkIdFromUri_0018, testing::ext::TestSize.Level1)
    {
        GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_GetNetWorkIdFromUri_0018";
        string fileUri = "file://media/image/7#networkId=3456";
        string fileNetWorkId = "3456";
        string networkId;
        int32_t ret = FileUri::GetNetWorkIdFromUri(fileUri, networkId);
        ASSERT_TRUE(ret == E_OK) << "networkId is " << networkId;
        EXPECT_EQ(networkId, fileNetWorkId);
        GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_GetNetWorkIdFromUri_0018";
    }

    /**
     * @tc.name: File_Uri_GetNetWorkIdFromUri_0019
     * @tc.desc: Test function of GetNetWorkIdFromUri() interface for success.
     * @tc.size: MEDIUM
     * @tc.type: FUNC
     * @tc.level Level 1
     * @tc.require: SR000H63TL
     */
    HWTEST_F(FileUriTest, File_Uri_GetNetWorkIdFromUri_0019, testing::ext::TestSize.Level1)
    {
        GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_GetNetWorkIdFromUri_0019";
        string fileUri = "file://media/image/12";
        string fileNetWorkId = "";
        string networkId;
        int32_t ret = FileUri::GetNetWorkIdFromUri(fileUri, networkId);
        ASSERT_TRUE(ret == E_OK) << "networkId is empty!";
        EXPECT_EQ(networkId, fileNetWorkId);
        GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_GetNetWorkIdFromUri_0019";
    }

    /**
     * @tc.name: File_Uri_GetNetWorkIdFromUri_0020
     * @tc.desc: Test function of GetNetWorkIdFromUri() interface for fail.
     * @tc.size: MEDIUM
     * @tc.type: FUNC
     * @tc.level Level 1
     * @tc.require: SR000H63TL
     */
    HWTEST_F(FileUriTest, File_Uri_GetNetWorkIdFromUri_0020, testing::ext::TestSize.Level1)
    {
        GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_GetNetWorkIdFromUri_0020";
        string fileUri = "data://media/image/12";
        string fileNetWorkId = "";
        string networkId;
        int32_t ret = FileUri::GetNetWorkIdFromUri(fileUri, networkId);
        ASSERT_TRUE(ret < E_OK) << "networkId is empty!";
        EXPECT_EQ(networkId, fileNetWorkId);
        GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_GetNetWorkIdFromUri_0020";
    }

    /**
     * @tc.name: File_Uri_Open_0021
     * @tc.desc: Test function of Open() interface for success.
     * @tc.size: MEDIUM
     * @tc.type: FUNC
     * @tc.level Level 1
     * @tc.require: SR000H63TL
     */
    HWTEST_F(FileUriTest, File_Uri_Open_0021, testing::ext::TestSize.Level1)
    {
        GTEST_LOG_(INFO) << "FileUriTest-begin File_Uri_Open_0021";
        string fileUri = "file://media/image/12";
        int32_t ret = FileUri::Open(fileUri, O_RDONLY);
        ASSERT_TRUE(ret > E_OK) << "fd get success, fd = " << to_string(ret);
        GTEST_LOG_(INFO) << "FileUriTest-end File_Uri_Open_0021";
    }
}